﻿using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using Facebook.Service;
using Facebook.Service.Core;

namespace FacebookPhotosLiveWriterPlugin
{
    public partial class PhotoAlbumsForm : System.Windows.Window
    {
        private List<Photo> selectedPhotos = new List<Photo>();

        public List<Photo> SelectedPhotos
        {
            get
            {
                return selectedPhotos;
            }
        }

        public PhotoAlbumsForm()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        private void btnInsert_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
            this.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            // Requests are done manually to ensure that user has photo albums to be loaded. 
            // This is a work-around due to a bug in Facebook.NET when the user has no Albums 
            FacebookRequest request = new FacebookRequest(FacebookPlugin.Service.Session);
            request.Parameters["uid"] =  FacebookPlugin.Service.UserID;
            FacebookResponse response = request.InvokeMethod("photos.getAlbums");
            if (response.Status == FacebookResponseStatus.Succeeded && response.Result is ArrayList)
            {
                ICollection<Album> albums = FacebookPlugin.Service.Photos.GetAlbumsOfUser(FacebookPlugin.Service.UserID);
                if (albums != null && albums.Count > 0)
                {
                    List<AlbumInfo> albumInfos = GetAlbumInfos(albums);
                    albumInfos.Insert(0,new AlbumInfo("Your photos", "0", "Insert from photos of you"));

                    Albums.ItemsSource = albumInfos;
                    Albums.SelectedIndex = 0;
                }
            }
        }

        private List<AlbumInfo> GetAlbumInfos(ICollection<Album> albums)
        {
            List<AlbumInfo> infos = new List<AlbumInfo>();
            foreach (Album album in albums)
            {
                infos.Add(new AlbumInfo(album.ID, album.Name, album.CoverID, album.Description));
            }
            return infos;
        }

        private void Border_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Border border = sender as Border;
            Photo photo = border.Tag as Photo;
            if (photo != null)
            {
                if (selectedPhotos.Contains(photo))
                {
                    selectedPhotos.Remove(photo);
                    border.Background = FindResource("imageItemBackround") as Brush;
                }
                else
                {
                    selectedPhotos.Add(photo);
                    border.Background = new SolidColorBrush(Colors.Silver);
                }
            }
        }

    }
}
